package com.tms.quartz.schdule;

import com.tms.domain.Tool;
import com.tms.quartz.service.QuartzService;
import com.tms.service.ToolService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时任务类，这个是开始做的时候的小实验，如果想要体验一下，加上注解@component即可
 *
 * @author ozj
 */
public class MySchedule {

    @Autowired
    QuartzService quartzService;

    @Autowired
    ToolService toolService;

    //@Scheduled(fixedDelay = 1000)
    public void fixedDelay() {
        System.out.println("fixedDelay: " + new Date());
    }

    //@Scheduled(fixedRate = 2000)
    public void fixedRate() {
        System.out.println("fixedRate: " + new Date());
    }

    @Scheduled(fixedRate = 1000)
    public void initialDelay() throws SchedulerException {
        System.out.println("initialDelay: " + new Date());
        Tool tool = toolService.findToolById(3);
        Map<String, String> map = new HashMap<>(2);
        map.put("toolId", String.valueOf(tool.getId()));
        quartzService.handerJob(tool, 2, "pause");
        quartzService.changeCycle(tool, 17);
    }

    @Scheduled(cron = "0 * * * * ?")
    public void corn() throws SchedulerException {
        System.out.println("cron: " + new Date());
        Tool tool = toolService.findToolById(2);
        Map<String, String> map = new HashMap<>(2);
        map.put("toolId", String.valueOf(tool.getId()));
        quartzService.addJobToTrigger(tool, 1, 20, map);

        //quartzService.changeCycle(tool,17);
    }

}
